﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.EntityClient;
using Simply.DataTier.Persistance;
using Simply.DataTier.Repository.Interface;
using Domain;
using System.Data.Linq;

namespace Simply.DataTier.Repository.Implementation
{
    public class BillingEFRepository : BaseEFRepository, IBillingRepository
    {
        /*private struct PrintPriceParams
        {
            public string productCode;
            public string mediaCode;
            public string methodCode; 
            public int quantity; 
            public DateTime date;
        }

        private struct FinishingPriceParams
        {
            public string productCode;
            public string finishingCode;
            public string mediaCode; 
            public int quantity; 
            public DateTime date;
        }

        private static readonly Func<SimplyDataContext, PrintPriceParams, PrintPricePolicyDO>
            GetPrintMinPriceQuery = CompiledQuery.Compile<SimplyDataContext, PrintPriceParams, PrintPricePolicyDO>(
                (ctx, printParams) => (from pol in
                                           ctx.PrintPricePolicies
                                       where pol.PrintMediaCode.Equals(printParams.mediaCode) &&
                                             pol.PrintMethodCode.Equals(printParams.methodCode) &&
                                             pol.ProductCode.Equals(printParams.productCode) &&
                                             pol.Quantity == printParams.quantity &&
                                            ((pol.ActiveFrom <= printParams.date && pol.ActiveTo >= printParams.date) ||
                                                (pol.ActiveFrom <= printParams.date && pol.ActiveTo == null))
                                       orderby pol.Price ascending
                                       select new PrintPricePolicyDO()
                                       {
                                           Id = pol.PrintPricePolicyId,
                                           Price = pol.Price,
                                           PrintMedia = new PrintMediaDO() { Code = pol.PrintMediaCode },
                                           PrintMethod = new PrintMethodDO() { Code = pol.PrintMethodCode },
                                           ProductType = pol.ProductCode,
                                           Quantity = pol.Quantity,
                                           ActiveFrom =pol.ActiveFrom,
                                           ActiveTo = pol.ActiveTo
                                       }).First()
            );

        private static readonly Func<SimplyDataContext, FinishingPriceParams, FinishingPricePolicyDO>
            GetFinishingMinPriceQuery = CompiledQuery.Compile<SimplyDataContext, FinishingPriceParams, FinishingPricePolicyDO>(
                (ctx, finishingParams) => (from pol in
                                               ctx.FinishingPricePolicies
                                           where 
                                           pol.FinishingServiceCode.Equals(finishingParams.finishingCode) &&
                                           pol.PrintMediaCode.Equals(finishingParams.mediaCode) &&
                                           pol.ProductCode.Equals(finishingParams.productCode) &&
                                           pol.Quantity == finishingParams.quantity &&
                                           ((pol.ActiveFrom <= finishingParams.date && pol.ActiveTo >= finishingParams.date) ||
                                           (pol.ActiveFrom <= finishingParams.date && pol.ActiveTo == null))
                                           orderby pol.Price ascending
                                           select new FinishingPricePolicyDO()
                                           {
                                               Id = pol.FinishingPricePolicyId,
                                               Price = pol.Price,
                                               PrintMedia = new PrintMediaDO() { Code = pol.PrintMediaCode },
                                               ProductType = pol.ProductCode,
                                               Quantity = pol.Quantity,
                                               ActiveFrom = pol.ActiveFrom,
                                               ActiveTo = pol.ActiveTo,
                                               FinishingService = new FinishingServiceDO() { Code = pol.FinishingServiceCode }
                                           }).First()
            );

        



        #region IBillingRepository Members

        public PricePolicyDO GetProductMinPricePolicy(long productId, int quantity, DateTime date)
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                PricePolicyDO policy = (from pol in context.PricePolicies
                                        from pr in pol.ProductToPricePolicies
                                        where pr.ProductId == productId &&
                                        ((pol.ActiveFrom <= date && pol.ActiveTo >= date) ||
                                        (pol.ActiveFrom <= date && pol.ActiveTo == null))
                                        orderby pol.Price ascending
                                        select new PricePolicyDO()
                                        {
                                            Id = pol.PricePolicyId,
                                            NameEn = pol.NameEn,
                                            NameRu = pol.NameRu,
                                            NameAm = pol.NameAm,
                                            Price = pol.Price,
                                            ActiveFrom = pol.ActiveFrom,
                                            ActiveTo = pol.ActiveTo
                                        }).First();

                return policy;
            }
            
        }

        public PrintPricePolicyDO GetPrintMinPricePolicy(string productCode, string mediaCode, 
                                                    string methodCode, int quantity, DateTime date)
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                PrintPricePolicyDO policy = GetPrintMinPriceQuery.Invoke(context,
                    new PrintPriceParams()
                    {
                        date = date,
                        mediaCode = mediaCode,
                        methodCode = methodCode,
                        productCode = productCode,
                        quantity = quantity
                    });
                return policy;
            }
        }

        public FinishingPricePolicyDO GetFinishingMinPricePolicy(string productCode, string finishingCode, string mediaCode, int quantity, DateTime date)
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                FinishingPricePolicyDO policy = GetFinishingMinPriceQuery.Invoke(context,
                    new FinishingPriceParams()
                    {
                        date = date,
                        finishingCode = finishingCode,
                        mediaCode = mediaCode,
                        productCode = productCode,
                        quantity = quantity
                    });

                return policy;
            }
        }

       

        #endregion*/
    }
}
